﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Vit.Common;
using Vit.Common.Extensions;
using Vit.Common.Providers;
using Vit.Common.Helpers;
using Vit.Web.Helpers;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Extensions;

namespace Vit.Web.SqlProviders
{
    public class SqlFriendlyUrlProvider : IFriendlyUrlProvider
    {
        private ICacheProvider cache;
        private const string CacheKey = "FriendUrls";
        private WebProvidersDb db = new WebProvidersDb();

        public SqlFriendlyUrlProvider(ICacheProvider cache)
        {
            this.cache = cache;
        }

        #region IFriendlyUrlProvider Members

        public string RewriteUrl(string url)
        {
            if (FriendUrls == null)
                return null;
            foreach (var u in FriendUrls)
            {
                if (u.IsRegex)
                {
                    if (Regex.IsMatch(url, u.UrlPattern, RegexOptions.IgnoreCase))
                        return Regex.Replace(url, u.UrlPattern, u.RewriteUrl);
                }
                else
                {
                    if (u.UrlPattern.Equals(url, StringComparison.OrdinalIgnoreCase))
                        return u.RewriteUrl;
                }
            }
            return null;
        }

        public void AddRewriteRule(string urlPattern, string rewriteTo, bool isRegex, bool updateIfExists)
        {
            if (db.FriendlyUrls.Any(o => o.UrlPattern == urlPattern) && updateIfExists)
            {
                var url = db.FriendlyUrls.First(o => o.UrlPattern == urlPattern);
                url.RewriteUrl = rewriteTo;
                url.IsRegex = isRegex;
            }
            else
            {
                db.FriendlyUrls.Add(new FriendlyUrl { ID = Guid.NewGuid().ToString(), UrlPattern = urlPattern, RewriteUrl = rewriteTo, IsRegex = isRegex });
            }
            db.SaveChanges();
            cache.Remove(CacheKey);
        }

        public void RemoveRewriteRule(string urlPattern)
        {
            var url = db.FriendlyUrls.FirstOrDefault(o => o.UrlPattern == urlPattern);
            if (url != null)
                db.FriendlyUrls.Remove(url);
            db.SaveChanges();
            cache.Remove(CacheKey);
        }

        #endregion

        #region IProvider Members

        public string Name
        {
            get { return "SqlFriendUrlProvider"; }
        }

        #endregion

        public IEnumerable<FriendlyUrl> FriendUrls
        {
            get
            {
                IEnumerable<FriendlyUrl> friendUrls = null;
                if (cache != null)
                    friendUrls = cache.GetData(CacheKey) as IEnumerable<FriendlyUrl>;
                if (friendUrls == null)
                {
                    WebProvidersDb ldb = new WebProvidersDb();
                    ldb.Configuration(false, false);
                    try
                    {
                        friendUrls = ldb.FriendlyUrls.ToList();
                    }
                    catch { }

                    if (cache != null)
                        cache.Add(CacheKey, friendUrls);
                    ldb.Dispose();
                }
                return friendUrls;
            }
        }
    }
}
